//
//  WZDownLoadViewModel.swift
//  weibo
//
//  Created by mac on 16/3/1.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import AFNetworking
class WZDownLoadViewModel: NSObject {
    
    lazy var statesArray: [WZCellViewModel] = [WZCellViewModel]()
    
    
    func loadData(max_id:Int,since_id:Int,finished:(isSuccess: Bool,count:Int)->()){
        
        let manager = WZHTTPClient.shareInstance
        
        let access_token = WZUserViewModel.shareInstance.access_token
        
        let parameter = ["access_token":access_token ?? "",
            "max_id":max_id,
            "since_id":since_id]
        
        
        manager.loadRequest(.GET, URLString: "https://api.weibo.com/2/statuses/home_timeline.json", parameters: parameter, success: { (JSON) -> () in
            if let dict = JSON as? [String:NSObject]{
                
                if  let statuArray = dict["statuses"] as? [[String:NSObject]]{
                    
                    var temArray = [WZCellViewModel]()
                    for statuModel in statuArray{
                        
                        let statuModel = WZStatesModel(dict: statuModel)
                        let cellViewModel = WZCellViewModel(model: statuModel)
                        temArray.append(cellViewModel)
                    }
                    
                    if max_id > 0{
                        self.statesArray = self.statesArray + temArray
                    }else{
                        self.statesArray = temArray + self.statesArray
                        
                        
                    }
                    
                    finished(isSuccess: true,count: temArray.count)
                    
                }
            }
            
            }) { (error) -> () in
                sleep(2)
                finished(isSuccess: false,count: 0)
        }
        
        
        
    }
    
}
